package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import user.User;
import user.UserList;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    response.setCharacterEncoding("utf-8");
	    String uid = new String(request.getParameter("uid").getBytes("iso-8859-1"), "utf-8");
		String password = new String(request.getParameter("password").getBytes("iso-8859-1"), "utf-8");
		HttpSession session = request.getSession();
		UserList users = (UserList) session.getAttribute("userList");
		List<User> list = users.getUsers();
		PrintWriter out = response.getWriter();
		for (User u : list) {
		    System.out.println(u.getUid() + ", " + uid);
		    if (uid.equals(u.getUid()) && password.equals(u.getPassword())) {
		        out.println("您好！" + uid + ",恭喜你登录成功！！！");
		        return;
		    }
		}
		out.println("用户名不存在或密码错误！！！");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
